# 只有element类型的才可以使用选择器 tag attrib text等

from lxml import etree
text = """
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
 <h1 class= '1'><a href="www.baidu.com">百度</a></h1>
 <h1 class= '2'>page title</h1>
 <h1 class= '2'>page title</h1>
 <li class="1"><a href="">baidu</a></li>
<li class="2"><a href="">jd</a></li>
<li class="3"><a href="">tb</a></li>

    <li class="odd">A</li>
    <li class="even">B</li>
    <li class="odd">C</li>
 
</body>
</html>
"""
# css选择器
# tree = etree.HTML(text)
# titles = tree.cssselect('title')
# for title in titles:
#     print(title.text)
#
# print('----------')
#
# hs = tree.cssselect('h1')
# for h in hs:
#     print(h.text,h.attrib,h.tag)
#
# print('----------')
#
# hs = tree.cssselect('h1')
# for h in hs:
#     print(h.text,h.attrib,h.tag)
#
#     a1 = h.cssselect('a')
#     for a in a1:
#         print(a.text,a.attrib,a.tag)



# xpath选择器



tree = etree.HTML(text)

# 路径 /根路径 //任意路径  ./当前路径 ../上一层路径  .// 当前路径下的任意路径
# html  = tree.xpath('/html')
# print(html[0].tag)
# title = tree.xpath('//title')
# print(title[0].text)

# href = tree.xpath('//a[@href]')
# print(href)

contain = tree.xpath('//li[contains(text(),"B")]/text()')
print(contain)


